package com.wan37.logic.tradingshop.handler;

import com.wan37.common.uuid.UUIDUtil;
import com.wan37.logic.component.Worker;
import com.wan37.logic.db.AsynDBManager;
import com.wan37.logic.email.EmailManager;
import com.wan37.logic.global.reference.SpringContextHolder;
import com.wan37.logic.handler.WorkerHandler;
import com.wan37.logic.tradingshop.TradingShopManager;
import com.wan37.logic.tradingshop.cache.TradingShopCache;
import com.wan37.logic.tradingshop.domain.vo.TradingShopRecordVO;
import com.wan37.logic.tradingshop.domain.vo.TradingShopVO;
import com.wan37.logic.tradingshop.service.TradingShopRecordService;
import com.wan37.logic.tradingshop.service.TradingShopService;
import com.wan37.logic.tradingshop.util.TradingShopRouteUtil;
import com.wan37.protobuf.constant.CommonTradingShop;
import com.wan37.protobuf.constant.ShopLabel;

import java.util.ArrayList;
import java.util.List;

/**
 * @author : luoyong
 * @date : 2020-08-06 21:26
 **/
public class FixedPriceResultHandler implements WorkerHandler {
    private String tradingShopVOId;
    private String userId;
    private long ingot;
    private TradingShopCache tradingShopCache;
    private EmailManager emailManager;
    private TradingShopManager tradingShopManager;
    private AsynDBManager asynDBManager;
    private TradingShopService tradingShopService;
    private TradingShopRecordService tradingShopRecordService;
    private TradingShopRouteUtil tradingShopRouteUtil;
    public FixedPriceResultHandler(String tradingShopVOId, String userId, long ingot){
        this.tradingShopService = SpringContextHolder.getBean("tradingShopService");
        this.tradingShopRecordService = SpringContextHolder.getBean("tradingShopRecordService");
        this.tradingShopCache = SpringContextHolder.getBean("tradingShopCache");
        this.asynDBManager = SpringContextHolder.getBean("asynDBManager");
        this.emailManager = SpringContextHolder.getBean("emailManager");
        this.tradingShopManager = SpringContextHolder.getBean("tradingShopManager");
        this.tradingShopRouteUtil = SpringContextHolder.getBean("tradingShopRouteUtil");
        this.tradingShopVOId = tradingShopVOId;
        this.userId = userId;
        this.ingot = ingot;
    }
    @Override
    public void execute(Worker worker) throws Exception {

        if (!tradingShopCache.getTradingShopInMap().containsKey(tradingShopVOId)){
            //竞拍失败进行邮件补偿处理
            List<String> userIdList = new ArrayList<>();
            userIdList.add(userId);
            emailManager.sendEmail("你想竞拍的物品失败了，被另一个金主爸爸抢了，元宝补偿返回，请领取查收！", "拍卖行",
                    userIdList, ShopLabel.MONEY_TYPE_INGOT, ingot);
            tradingShopRouteUtil.sendTradingShopResponse(CommonTradingShop.ERROR,
                    "你手慢了哦，已经别另一个金主爸爸买走了哦！元宝补偿返回邮件了，请领取查收！", userId);
            return;
        }
        TradingShopVO tradingShopVO = tradingShopCache.getTradingShopInMap().get(tradingShopVOId).getTradingShopVO();
        //将上架方元宝获得进行邮件发送
        List<String> userIdList = new ArrayList<>();
        userIdList.add(tradingShopVO.getUserId());
        emailManager.sendEmail("竞拍结果获得元宝", "拍卖行",
                userIdList, ShopLabel.MONEY_TYPE_INGOT, tradingShopVO.getIngot());
        //邮件发送竞拍得到的装备
        tradingShopManager.addBuyerWeaponry(userId, "拍卖物品的结算，您竞拍成功。请注意查收",tradingShopVO);
        //创建交易记录，并且存库
        TradingShopRecordVO tradingShopRecordVO = new TradingShopRecordVO();
        tradingShopRecordVO.setUserId(tradingShopVO.getUserId());
        tradingShopRecordVO.setBuyId(userId);
        tradingShopRecordVO.setCreateTime(System.currentTimeMillis());
        tradingShopRecordVO.setRecordId(UUIDUtil.getUUID());
        tradingShopRecordVO.setIngot(tradingShopVO.getIngot());
        tradingShopRecordVO.setTradingShopId(tradingShopVO.getTradingShopId());
        asynDBManager.distributeWork(tradingShopVOId, ()->tradingShopRecordService.save(tradingShopRecordVO));
        //将交易物从缓存中删去
        tradingShopCache.getTradingShopInMap().remove(tradingShopVO.getTradingShopId());
        //从数据库中删去
        asynDBManager.distributeWork(tradingShopVOId, ()->
                tradingShopService.deleteByTradingShopId(tradingShopVO.getTradingShopId()));
        tradingShopManager.getAll(userId);
        tradingShopRouteUtil.sendTradingShopResponse(CommonTradingShop.BUY_FIXED_PRICE,
                "竞拍成功,稍后会物品会发送至邮箱，请注意查收！", userId);
    }
}
